/**
 * Created by LeungZ on 2016/10/17.
 */
class Deployment {
    //控制器
    constructor($scope, ctgNotification, appSrv, $state, appImgList, groupList) {
        "ngInject";      
        $scope.$parent.vm.step = 1;
        this._notification = ctgNotification;
        this._fData = $scope.$parent.vm.fData;
        this._appSrv = appSrv;
        this._state = $state;        
        this.currentForm = 'vol';  //资源配置
        this._fData.cluster={
            clusterCode:$state.params.k8sCode
        }
        this._fData.namespaceCode=$state.params.lesseeCode;
        this.podCpu=appSrv._cpulimit;
        this.podMem=appSrv._memorylimit;
        this.podPort=appSrv._dfPort;
        if (!appSrv._cpulimit){
            this.CpuInfo();
        }
        if (appImgList) this.appImgList = appImgList.list;
        if (groupList) this.groupList = groupList.list;
        
        if (this._fData.services) {
            angular.extend(this, this._fData.services[0]);
        }        
    }
    // 路由数据
    CpuInfo(){
        this._appSrv.getConfig().then(data=>{
        this.podPort = data['deployment.externalPort'];
        this.podMem = Number(data['memory.limit']);
        this.podCpu = Number(data['cpu.limit']);
        }).catch(data=>{
           this._notification.error('cpu数据有误');
        })
    }
    checkServiceValidate() {
        if (this.podAppName) {
            this.serviceCodeChecking = true;
            this._appSrv.checkServiceCode({
                appId: this._fData.app.id,
                serviceCode: this.podAppName
            }).then(data=> {
                this.$forms.base.podAppName.$setValidity('duplicate', !data.exist);
                this.$forms.base.podAppName.$setValidity('network', true);
            }).catch(data=> {
                this.$forms.base.podAppName.$setValidity('network', false);
            }).finally(()=> {
                this.serviceCodeChecking = false;
            });
        }
    }

    next() {
        if (this.serviceCodeChecking || this._appSrv.checkInvalid(this.$forms)) return;

        this.loading = true;
        this._fData.services = [this];
        this._appSrv.getYaml(this._fData).then(data=> {
            this._fData.yJsonList = data;
            this._state.go('app-service-version-publish.release');
        }).catch((data)=> {
              if(data.resultCode==1200){
                this._notification.error(data.resultMsg);
            }else{
            this._notification.error("加载Yaml配置失败.");
            }
        }).finally(()=> {
            this.loading = false;
        })

    }
}
export default Deployment